#include "stdafx.h"
#include "ui_resource_render.h"
#include "ui_gdi_render_util.h"
#include "ui_freetype_render_util.h"
#include "ui_gdiplus_render_util.h"

_SSL_BEGIN
_SSUIL_BEGIN

UIResourceRender::UIResourceRender()
{

}

UIResourceRender::~UIResourceRender()
{

}

bool UIResourceRender::RecordFont(
	const tstring &id,
	const tstring &font_name,
	int size,
	bool bold,
	bool underline,
	bool italic
	)
{
#ifdef USE_FREETYPE_FONT
	UIFtFontIdInfo id_info;
	id_info.bold = bold ? 1 : 0;
	id_info.size = size;
	id_info.underline = underline ? 1 : 0;
	id_info.italic = italic ? 1 : 0;
	id_info.font_face_name = font_name;
	fonts_.AddFontId(id, id_info);
	return true;
#else
	return gdi_fonts_.AddFont(id.c_str(), font_name.c_str(), size, bold, underline, italic);
#endif
}

bool UIResourceRender::SetDefaultFont(const tstring &id)
{
#ifdef USE_FREETYPE_FONT
	fonts_.SetDefaultFont(id);
	return true;
#else
	return gdi_fonts_.SetDefaultFont(id.c_str());
#endif
}

void UIResourceRender::RecordNameValuePair(
	const tstring &name,
	const tstring  &type,
	const tstring &value
	)
{
	if(name.empty())
		return;
	name_values_[name].type = type;
	name_values_[name].val = value;
}

bool UIResourceRender::QueryNameValuePair(
	const tstring &name,
	ss_out_ tstring &type,
	ss_out_ tstring &value
	)
{
	name_value_pair_map::iterator iter = name_values_.find(name);
	if(iter == name_values_.end())
		return false;
	type = iter->second.type;
	value = iter->second.val;
	return true;
}

bool UIResourceRender::CheckPresetImageInfo(
	ss_in_ const UIPresetImage &preset_img,
	ss_out_ UIPresetImageInfo &info
	)
{
	return images_.CheckPresetImageInfo(preset_img, info);
}

bool UIResourceRender::CheckImageDInfo(
	ss_in_ const UIImageD &img_data,
	ss_out_ UIPresetImageInfo &info
	)
{
	return images_.CheckImageDInfo(img_data, info);
}

UIImageD UIResourceRender::GenerateImageFromHandle(HBITMAP bitmap)
{
	return images_.GenerateImageFromHandle(bitmap);
}

void UIResourceRender::DestroyImage(UIImageD &img)
{
	return images_.ReleaseImage(img);
}

void UIResourceRender::DrawColor(void *dc, const UIRect &dest_rt, unsigned long color)
{
	return UIGdiRenderUtil::DrawColor((HDC)dc, dest_rt, color);
}

void UIResourceRender::DrawText(void *dc, const tstring &text, int len,
	unsigned long color, const UIRect &rt, const tstring &font_id,
	unsigned int style, bool transparent, unsigned long bk_color)
{
#ifdef USE_FREETYPE_FONT
	return UIFreetypeRenderUtil::DrawText(&fonts_, (HDC)dc,
		text.c_str(), len, color, &rt, font_id, style, transparent, bk_color);
#else
	UIFontD font = gdi_fonts_.GetFont(font_id.c_str());
	if(font.font == NULL)
		return;

	return UIGdiRenderUtil::DrawText((HDC)dc, text.c_str(), len, color, &rt, (HFONT)font.font, style,
		transparent, bk_color);
#endif
}

bool UIResourceRender::MeasureTextSize(const tstring &text, int len, ss_inout_ UIRect &rt,
	const tstring &font_id, UIMeasureTextType measure_type)
{
#ifdef USE_FREETYPE_FONT
	return UIFreetypeRenderUtil::MeasureTextSize(&fonts_, text.c_str(), len, rt, font_id, measure_type);
#else
	UIFontD font = gdi_fonts_.GetFont(font_id.c_str());
	if(font.font == NULL)
		return false;
	return UIGdiRenderUtil::MeasureTextSize(text.c_str(), len, rt, (HFONT)font.font, measure_type);
#endif
}

void UIResourceRender::DrawImage(void *dc, const tstring &img_name, const UIPoint &dest_pt,
	float rotate /*= 0*/, unsigned int alpha_value /*= 0xff*/)
{
	if(img_name.empty())
		return;

	UIImageD image = QueryImage(img_name);
	SS_ASSERT(image.image_type == images_.GetImageType());

	return DrawImage(dc, image, dest_pt, rotate, alpha_value);
}

void UIResourceRender::DrawImage(void *dc, const UIImageD &img, const UIPoint &dest_pt,
	float rotate/*= 0*/, unsigned int alpha_value /*= 0xff*/)
{
	if(img.image == NULL)
		return;
	if(img.image_type != images_.GetImageType())
		return;

	UIGdiplusRenderUtil::DrawImage((HDC)dc, (Gdiplus::Image*)img.image, dest_pt, rotate, alpha_value);
}

void UIResourceRender::DrawImage(void *dc, const tstring &img_name, const UIRect *src_rt,
	const UIRect &dest_rt, const UIRect *border_clip, DWORD mask /*= 0*/,
	float rotate /*= 0*/, unsigned int alpha_value /*= 0xff*/)
{
	if(img_name.empty())
		return;

	UIImageD image = QueryImage(img_name);
	SS_ASSERT(image.image_type == images_.GetImageType());

	return DrawImage(dc, image, src_rt, dest_rt, border_clip, mask, rotate, alpha_value);
}

void UIResourceRender::DrawImage(void *dc, const UIImageD &img, const UIRect *src_rt,
	const UIRect &dest_rt, const UIRect *border_clip, DWORD mask /*= 0*/,
	float rotate /*= 0*/, unsigned int alpha_value /*= 0xff*/)
{
	HDC dc_handle = (HDC)dc;
	if(::GetObjectType(dc_handle) != OBJ_DC && ::GetObjectType(dc_handle) != OBJ_MEMDC)
		return;

	if(img.image == NULL)
		return;
	UIImageInfoData *img_info_data = (UIImageInfoData*)img.image;
	if(img_info_data->image_h_ == NULL)
		return;

	Gdiplus::Image* image = (Gdiplus::Image *)img_info_data->image_h_;

	UIRect calced_src_image_rt;
	if(src_rt == NULL)
	{
		src_rt = &calced_src_image_rt;
		calced_src_image_rt.left = 0;
		calced_src_image_rt.top = 0;
		calced_src_image_rt.right = image->GetWidth();
		calced_src_image_rt.bottom = image->GetHeight();
	}

	DWORD b_left = 0, b_right = 0, b_top = 0, b_bottom = 0;
	if(border_clip != NULL)
	{
		b_left = border_clip->left;
		b_right = border_clip->right;
		b_top = border_clip->top;
		b_bottom = border_clip->bottom;
	}
	if((LONG)(b_left + b_right) >= src_rt->Width() ||
		(LONG)(b_left + b_right) >= dest_rt.Width())
	{
		b_left = 0;
		b_right = 0;
	}

	if((LONG)(b_top + b_bottom) >= src_rt->Height() ||
		(LONG)(b_top + b_bottom) >= dest_rt.Height())
	{
		b_top = 0;
		b_bottom = 0;
	}

	UIRect rtImgs[9], rtDsts[9];
	rtImgs[0].left = src_rt->left;
	rtImgs[0].top = src_rt->top;
	rtImgs[0].right = rtImgs[0].left + b_left;
	rtImgs[0].bottom = rtImgs[0].top + b_top;

	rtDsts[0].left = dest_rt.left;
	rtDsts[0].top = dest_rt.top;
	rtDsts[0].right = rtDsts[0].left + b_left;
	rtDsts[0].bottom = rtDsts[0].top + b_top;

	rtImgs[1] = rtImgs[0];
	rtImgs[1].left = rtImgs[0].right;
	rtImgs[1].right = src_rt->right - b_right;

	rtDsts[1] = rtDsts[0];
	rtDsts[1].left = rtDsts[0].right;
	rtDsts[1].right = dest_rt.right - b_right;

	rtImgs[2] = rtImgs[1];
	rtImgs[2].left = rtImgs[1].right;
	rtImgs[2].right = rtImgs[2].left + b_right;

	rtDsts[2] = rtDsts[1];
	rtDsts[2].left = rtDsts[1].right;
	rtDsts[2].right = rtDsts[2].left + b_right;

	rtImgs[3] = rtImgs[0];
	rtImgs[3].top = rtImgs[0].bottom;
	rtImgs[3].bottom =src_rt->bottom - b_bottom;

	rtDsts[3] = rtDsts[0];
	rtDsts[3].top = rtDsts[0].bottom;
	rtDsts[3].bottom = dest_rt.bottom - b_bottom;

	rtImgs[4] = rtImgs[3];
	rtImgs[4].left = rtImgs[3].right;
	rtImgs[4].right = rtImgs[1].right;

	rtDsts[4] = rtDsts[3];
	rtDsts[4].left = rtDsts[3].right;
	rtDsts[4].right = rtDsts[1].right;

	rtImgs[5] = rtImgs[4];
	rtImgs[5].left = rtImgs[4].right;
	rtImgs[5].right = rtImgs[2].right;

	rtDsts[5] = rtDsts[4];
	rtDsts[5].left = rtDsts[4].right;
	rtDsts[5].right = rtDsts[2].right;

	rtImgs[6] = rtImgs[3];
	rtImgs[6].top = rtImgs[3].bottom;
	rtImgs[6].bottom =src_rt->bottom;

	rtDsts[6] = rtDsts[3];
	rtDsts[6].top = rtDsts[3].bottom;
	rtDsts[6].bottom = dest_rt.bottom;

	rtImgs[7] = rtImgs[6];
	rtImgs[7].left = rtImgs[6].right;
	rtImgs[7].right = rtImgs[4].right;

	rtDsts[7] = rtDsts[6];
	rtDsts[7].left = rtDsts[6].right;
	rtDsts[7].right = rtDsts[4].right;

	rtImgs[8] = rtImgs[7];
	rtImgs[8].left = rtImgs[7].right;
	rtImgs[8].right = rtImgs[5].right;

	rtDsts[8] = rtDsts[7];
	rtDsts[8].left = rtDsts[7].right;
	rtDsts[8].right = rtDsts[5].right;

	UIGdiplusRenderUtil::Draw9GridImage(dc_handle, image, rtImgs, rtDsts, (WORD)mask, alpha_value);
}

void UIResourceRender::DrawFrame(void *dc, const UIRect &dest_rt, unsigned long color)
{
	UIGdiRenderUtil::DrawFrame((HDC)dc, dest_rt, color);
}

scoped_ref_ptr<IUIDiskFilesManagerInterface> UIResourceRender::SetDiskFilesManager(
	IUIDiskFilesManagerInterface * disk_man)
{
	return images_.SetDiskFilesManager(disk_man);
}

scoped_ref_ptr<IUIDiskFilesManagerInterface> UIResourceRender::GetDiskFilesManager()
{
	return images_.GetDiskFilesManager();
}

UIImageD UIResourceRender::QueryImage(const tstring &img_name)
{
	return images_.GetImage(img_name);
}

_SSUIL_END
_SSL_END
